package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.Aftersale;
import com.wn.class30.entity.User;
import com.wn.class30.service.AfterSaleService;
import com.wn.class30.service.BackUserService;
import com.wn.class30.service.OrderService;
import com.wn.class30.tools.Page;
import com.wn.class30.tools.Result;
import com.wn.class30.tools.StringToArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/houtai/page/order")
public class AfterSaleController {

    @Resource
    private AfterSaleService afterSaleService;
    @Resource
    private BackUserService backUserService;

    /**
     * 根据条件分页查询售后表
     * @param page
     * @param limit
     * @return
     * @throws Exception
     */
    @RequestMapping("/aftersalelist")
    public Result selectAfterSales(Integer page,Integer limit, String myorderNumber, String username, String auditingstatus, String startDate,String endDate)throws Exception{
        Map<String,String> map = new HashMap<>();
        map.put("page",String.valueOf((page-1)*limit));
        map.put("limit",String.valueOf(limit));
        map.put("myorderNumber", myorderNumber);
        map.put("username", username);
        map.put("auditingstatus", auditingstatus);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        Page p = new Page();
        p.setPageSize(limit);
        p.setCurrentPage(page);
        Long afterSaleCount = afterSaleService.getAfterSaleCount(map);
        p.setTotalSize(afterSaleCount);
        p.setTotalPage((int) (afterSaleCount % limit == 0?afterSaleCount/limit:afterSaleCount/limit+1));
        List<Aftersale> aftersales = afterSaleService.selectAfterSales(map);
        for (Aftersale aftersale : aftersales) {
            User user = backUserService.selectUserByUserId(aftersale.getUserId());
            aftersale.setUser(user);
        }
        return new Result("0","success",p,aftersales);
    }

    /**
     * 通过审核
     * @param aftersaleIds
     * @return
     * @throws Exception
     */
    @RequestMapping("passMany")
    public Result passMany(String aftersaleIds)throws Exception{
        if(aftersaleIds != null && !"".equals(aftersaleIds)){
            Integer[] array = StringToArray.getArray(aftersaleIds);
            for (Integer integer : array) {
                Aftersale aftersale = afterSaleService.selectAfterSaleById(integer);
                aftersale.setAuditingstatus(2);
                afterSaleService.updateAfterById(aftersale);
            }
            return new Result("200","success",null,null);
        }
        return new Result("500","error",null,null);
    }

    @RequestMapping("refusedMany")
    public Result refusedMany(String aftersaleIds)throws Exception{
        if(aftersaleIds != null && !"".equals(aftersaleIds)){
            Integer[] array = StringToArray.getArray(aftersaleIds);
            for (Integer integer : array) {
                Aftersale aftersale = afterSaleService.selectAfterSaleById(integer);
                aftersale.setAuditingstatus(3);
                afterSaleService.updateAfterById(aftersale);
            }
            return new Result("200","success",null,null);
        }
        return new Result("500","error",null,null);
    }
}
